<template>
  <!-- 重置密码页面 -->
  <div>
    <el-form
      ref="resetPsdRef"
      :model="dataForm"
      :rules="rules"
      label-width="80px"
      status-icon
      class="login-form"
      @keyup.enter.native="onSubmit()"
    >
      <h3 class="title">修改密码</h3>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="请输入用户名" clear />
      </el-form-item>
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input
          v-model="dataForm.oldPassword"
          placeholder="请输入旧密码"
          type="password"
          clear
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          v-model="dataForm.newPassword"
          placeholder="请输入新密码"
          type="password"
          clear
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="submitPassword">
        <el-input
          v-model="dataForm.submitPassword"
          placeholder="请输入确认密码"
          type="password"
          clear
        ></el-input>
      </el-form-item>
      <div class="login-btn">
        <el-button type="primary" @click="onSubmit()">提交</el-button>
        <el-button @click="onResetBtn()">重置</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { resetPsd } from '@/api/user';
export default {
  name: 'ResetPassword',
  data() {
    const validNewPsd = (rule, value, callback) => {
      if (!value.trim()) {
        return callback(new Error('新密码不能为空'));
      } else if (value.trim().length < 3 || value.trim().length > 15) {
        return callback(new Error('新密码长度为3-15'));
      } else if (value === this.dataForm.oldPassword) {
        return callback(new Error('新旧密码不能一致!'));
      } else {
        if (this.dataForm.submitPassword !== '') {
          this.$refs.resetPsdRef.validateField('submitPassword');
        }
        callback();
      }
    };
    const validPsd = (rule, value, callback) => {
      if (!value.trim()) {
        return callback(new Error('确认密码不能为空'));
      } else if (value !== this.dataForm.newPassword) {
        return callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      dataForm: {
        username: '',
        oldPassword: '',
        newPassword: '',
        submitPassword: '',
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度为3-15' },
        ],
        oldPassword: [
          { required: true, message: '旧密码不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '旧密码长度为3-15' },
        ],
        newPassword: [
          { required: true, message: '新密码不能为空', trigger: 'blur' },
          { validator: validNewPsd, trigger: 'blur' },
        ],
        submitPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: validPsd, trigger: 'blur' },
        ],
      },
    };
  },
  created() {
    let userInfo = JSON.parse(sessionStorage.getItem('userInfo') || {});
    this.dataForm.id = userInfo.id;
    this.dataForm.username = userInfo.username;
  },
  methods: {
    onSubmit() {
      this.$refs['resetPsdRef'].validate((valid) => {
        if (valid) {
          resetPsd(this.dataForm)
            .then((res) => {
              if (res && res.code === 0) {
                sessionStorage.setItem('userInfo', JSON.stringify(res.userEntity));
                this.$message.success('密码修改成功');
                this.onResetBtn();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              console.log('err, ', err);
              this.$message.error('系统错误，请联系管理员');
            });
        }
      });
    },
    onResetBtn() {
      this.$refs.resetPsdRef.resetFields();
    },
  },
};
</script>

<style scoped>
.title {
  margin-bottom: 20px;
}
.login-btn {
  text-align: center;
}
</style>
